

/*
import game.SceneManager.SCENE;

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.util.Random;

public class Game 
{
	private Canvas canvas;
	
	private static Game instance;
	
	long lastTime;
	
	public static double loopTime;
	
	int fps = 60;
	long a, b;
	
	Image image;
	float x, y;
	int w, h;
	
	
	public static final int screenWidth  = 800;
	public static final int screenHeigth = 600;
	
	public static Random rand = new Random();
	public static int intPause = 1;
	float time;
	boolean escapePressed = false;
	
	
	private Game()
	{
		GraphicsEnvironment ge   = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gd        = ge.getDefaultScreenDevice();
		GraphicsConfiguration gc = gd.getDefaultConfiguration();	
		rand = new Random();
		this.canvas = new Canvas(gc);

		SceneManager.setup();
		
		this.lastTime = System.nanoTime();
		
		this.x = this.y = 0;
		this.w = 800;
		this.h = 600;
		
		instance = null;
	}
	
	public static Game getInstance()
	{
		if (instance == null)
		{
			instance = new Game();			
		}
		return instance;
	}
	
	public Canvas getCanvas() 
	{
		return canvas;
	}
	
	public void run()   
	{	
		long currentTime = 0;
		
		//SceneManager.setup();
		
		while (true)
		{
			currentTime = System.nanoTime();
			
			//System.out.println("NANO TIME: " + System.nanoTime()/1000000000000000f);
			//System.out.println("CurrentTime: " + currentTime);
						
			//Update
			
			
			//Draw
			draw((Graphics2D)this.canvas.getGraphics());
			
			this.canvas.swapBuffers();
			this.canvas.repaint();
								
			this.lastTime = currentTime;
						
			// FPS
			a = System.nanoTime();
			
			fps++;
			if (a - b >= 1000000000)
			{
				loopTime = (double)((1.0)/fps);
				this.canvas.setTitle("The Legend of Silicon - fps: " + fps);
				fps = 1;
				b = a;
			}			
			
			try 
			{
				Thread.sleep(15);
			}
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
			update();
		}
	}


	public void update()
	{
		time += loopTime; 
		
		if(intPause == 1)
		{
			SceneManager.scene.update();
			//SoundClip.removeOldSounds();
		}
		if(	SceneManager.currentScene == SCENE.SCN_LEVEL_01 ||
			SceneManager.currentScene == SCENE.SCN_LEVEL_02 ||
			SceneManager.currentScene == SCENE.SCN_LEVEL_03 ||
			SceneManager.currentScene == SCENE.SCN_LEVEL_04 ||
			SceneManager.currentScene == SCENE.SCN_LEVEL_05)
		{
			
			if(intPause == 1)
			{
				
				if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_ESCAPE) && time > 0.1f && !escapePressed)
				{
					escapePressed = true;
					intPause = 0;
					time = 0;
				}
			}
			else
			{
				if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_ESCAPE) && time>0.1f && !escapePressed)
				{
					escapePressed = true;
					intPause = 1;
					time = 0;
					
					
				}
				if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_M))
				{
					//SoundClip.StopAll();
					Audios.getInstance().stopAll();
					intPause = 1;
					SceneManager.changeScene(0);
				}
			}
			
			if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_ESCAPE) == false)
				escapePressed = false;
		}
	}

	public void draw(Graphics2D g2d)
	{
		SceneManager.scene.draw(g2d);
	}
}



*/


package game;

import game.SceneManager.SCENE;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.*;
import java.applet.*;
import java.util.*;


public class Game extends Applet implements Runnable
{

	private static final long serialVersionUID = 1L;
	
	public static final int screenWidth  = 800;
	public static final int screenHeigth = 600;
	static Random rand = new Random();
	
	
	
	BufferedImage backbuffer;
	Graphics2D g2d;
	Thread gameLoop;
	static Game instance;
	
	Background pause;
	
	
	public boolean jogoPausado = false;
	
	float time;
	boolean escapePressed = false;
	boolean spacePressed = false;
	
	
	
	public void init()
	{
		
		this.backbuffer = new BufferedImage(screenWidth, screenHeigth, BufferedImage.TYPE_INT_RGB);
		this.g2d = backbuffer.createGraphics();
		addKeyListener(Keyboard.getInstance());
		//setFocusable(true);
		SceneManager.setup();
	}

	public void start()
	{
		this.gameLoop = new Thread(this);
		this.gameLoop.start();
	}

	public void stop()
	{
		this.gameLoop = null;
	}

	public void run()
	{
		Thread t = Thread.currentThread();

		while (t == this.gameLoop)
		{
			Time.getInstance().UpdateLastTime();
			try 
			{
				Thread.sleep(15);
			}
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
			repaint();
			Time.getInstance().UpdateLoopTime();
		}	
	}
		
	
	
	public void paint(Graphics g)
	{	
		SceneManager.scene.draw(g2d);	
		if(this.jogoPausado)
		{
			if(pause == null)
				pause = new Background(34);
			
			pause.draw(g2d);
		}
		g.drawImage(backbuffer, 0, 0, this);
	}
	
	
	
	public void update(Graphics g)
	{
		time += Time.getInstance().getLoopTime(); 
		
		if(this.jogoPausado)
		{
			if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_ESCAPE))
			{
				if(this.escapePressed == false)
				{
					this.jogoPausado = false;
					SceneManager.changeScene(0);
				}
			}
			else
				this.escapePressed = false;
			
			if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_SPACE))
			{
				if(this.spacePressed == false)
					this.jogoPausado = false;
			}
			else
				this.spacePressed = false;
		}
		else
		{
			SceneManager.scene.update();
			
			if(SceneManager.getLevel() != 0 && 
					SceneManager.currentScene != SceneManager.SCENE.SCN_CONGRATS &&
					SceneManager.currentScene != SceneManager.SCENE.SCN_GAMEOVER &&
					SceneManager.currentScene != SceneManager.SCENE.SCN_COMPLETEDLVL &&
					SceneManager.currentScene != SceneManager.SCENE.SCN_NEWLVL					
					)
			{
				if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_ESCAPE))
				{
					if(escapePressed == false)
					{
						this.escapePressed = true;
						this.jogoPausado = true;
					}
				}
				else
					this.escapePressed = false;
			}
			
		}
		paint(g);
		
	}
}


